// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env:'mupp-5gs2863z7cf6873f'
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  let res = await db.collection('comment').aggregate()
  .end()
  let items = res.list
  let openid = event.openid
  console.log(items)
  items.reverse()  //将查询到的数据倒序 这样先发布的内容就会显示在上面
  items.forEach(v=>{
    if(v.dz.indexOf(openid)>-1){   //判断这个文章是否有点赞
      v.dianzan = 'dianzan.png'
   }else{
    v.dianzan = 'aixin.png'
   }
  })
  console.log(items)
  let leftitems = []
  let rightitems = []
  items.forEach( (item, index)=> {  //分割为两个数组
    if (index % 2) {
      rightitems.push(item)
    } else {
      leftitems.push(item)
    }
})
  console.log(leftitems)
  console.log(rightitems)
  return {
    leftitems,
    rightitems
  }
}